<?php
class Login {
	
	/**
	 * <h2>日志对象</h2>
	 *
	 * @var Logger
	 */
	private $_logger;
	
	/**
	 * <h2>构造函数</h2>
	 * <p>完成初始化，日志对象的创建<p>
	 */
	public function __construct() {
		Logger::configure ( ROOT_PATH . 'config/log_config.xml' );
		$this->_logger = Logger::getLogger ( __CLASS__ );
	}
	
	/**
	 * <h2>后台登录的默认动作</h2>
	 */
	public function index() {
		if (isAdminLogin ()) {
			forwardToHome ();
		}
		// 如果没有登录的话，就显示登录界面
		include 'templates/login.html';
	}
	
	/**
	 * <h2>检查后台登录</h2>
	 *
	 * @throws ExceptionEx
	 */
	public function check() {
		$request = new Request ();
		$email = new CEMAIL($request->getPost ( 'username' ));
		$password = $request->getPost ( 'password' );
		
		try {
			if (!$email->isValid() || empty ( $password )) {
				throw new ExceptionEx ( 'Username & Password cannot be empty' );
			}

			$identity = new CMAP();
			$identity->put('email', $email->getValue());
			$identity->put('password',$password);
			$auth = new Auth();
			$ret = $auth->authLogin($identity, $uid);
			if (STATUS_SUCCESS !== $ret) {
				throw new ExceptionEx('Username & password is invalidate');
			}
			$_SESSION['admin'] = true;
			forwardToHome();
		} catch ( ExceptionEx $e ) {
			header ( 'Location:' . CONSOLE_BASE . '?a=login' );
			exit (); // 记得结束，否则后边的代码会继续执行
		}
	}
}